using Jupiter.Infrastructure.Permission.Attributes;
using Jupiter.Infrastructure.Responses;
using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Wips.AppService.Authorities.Menus;
using Wips.AppService.Authorities.Menus.Requests;
using Wips.AppService.Authorities.Menus.Responses;
using Wips.AppService.Authorities.Menus.ViewModels;
using Wips.AppService.CommonViewModels;

namespace Wips.MainServer.Controllers.Authorities
{
    /// <summary>
    /// 菜单
    /// </summary>
    public class MenuController : CustomeBaseController
    {
        private readonly IMenuService _service;
        private readonly IMediator _mediator;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="service"></param>
        /// <param name="mediator"></param>
        public MenuController(
            IMenuService service,
            IMediator mediator)
        {
            _service = service;
            _mediator = mediator;
        }

        #region 基础接口

        /// <summary>
        /// 读取列表
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost]
        [Permission]
        public async Task<PageResult<GetMenuPagesResponse>> GetPagesAsync(
            [FromBody] GetMenuPagesRequest request)
        {
            return await _service.GetPagesAsync(request);
        }

        /// <summary>
        /// 根据ID读取
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        [Permission]
        public async Task<MenuViewModel> GetAsync([FromQuery] string id)
        {
            return await _service.GetByIdAsync(id);
        }

        /// <summary>
        /// 创建
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost]
        [Permission]
        public async Task<string> PostAsync([FromBody] CreateMenuRequest request)
        {
            return await _mediator.Send(request);
        }

        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPut]
        [Permission]
        public async Task<string> PutAsync([FromBody] UpdateMenuRequest request)
        {
            return await _mediator.Send(request);
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpDelete]
        [Permission]
        public async Task<string> DeleteAsync([FromBody] DeleteMenuRequest request)
        {
            return await _mediator.Send(request);
        }

        /// <summary>
        /// 状态变更
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPut]
        [Permission]
        public async Task<string> StatusChangeAsync([FromBody] MenuStatusChangeRequest request)
        {
            return await _mediator.Send(request);
        }

        #endregion

        #region 扩展接口

        /// <summary>
        /// 读取树形菜单列表
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<List<TreeMenuViewModel>> GetTreeMenuByUserIdAsync()
        {
            return await _service.GetTreeMenuByUserIdAsync();
        }

        /// <summary>
        /// 获取用户路由
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [AllowAnonymous]
        public async Task<List<MenuItemViewModel>> GetUserRoutesAsync()
        {
            return await _service.GetUserRoutesAsync();
        }

        /// <summary>
        /// 读取树形选择框数据列表
        /// </summary>
        /// <param name="roleId">角色Id</param>
        /// <returns></returns>
        [HttpGet]
        [Permission]
        public async Task<GetTreeSelectDataByRoleIdResponse> GetTreeSelectDataByRoleIdAsync(
            [FromQuery] string roleId)
        {
            return await _service.GetTreeSelectDataByRoleIdAsync(roleId);
        }

        /// <summary>
        /// 读取树形结构的菜单列表
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Permission]
        public async Task<List<TreeSelectViewModel>> GetTreeSelectDataAsync()
        {
            return await _service.GetTreeSelectDataAsync();
        }

        /// <summary>
        /// 读取树形列表
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Permission]
        public async Task<List<GetMenuTreeDataResponse>> GetTreeDataAsync()
        {
            return await _service.GetTreeDataAsync();
        }

        /// <summary>
        /// 读取树形选择框数据列表(by 版本和应用)
        /// </summary>
        /// <param name="versionId">版本id </param>
        /// <returns></returns>
        [HttpGet]
        [Permission]
        public async Task<GetTreeSelectDataByRoleIdResponse> GetTreeSelectDataByVersionAsync(
            string versionId)
        {
            return await _service.GetTreeSelectDataByVersionAsync(versionId);
        }


        /// <summary>
        /// 读取树形表格数据by(角色)
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Permission]
        public async Task<GetTreeTableResponse> GetTreeTableByRoleIdAsync(string roleId)
        {
            return await _service.GetTreeTableByRoleIdAsync(roleId);
        }


        /// <summary>
        /// 读取树形表格数据
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Permission]
        public async Task<GetTreeTableResponse> GetTreeTableByVersionsIdAsync(string versionId)
        {
            return await _service.GetTreeTableByVersionsIdAsync(versionId);
        }

        #endregion
    }
}